<template>
    <div class="content-wrap">
        <div class="content-top-box">
            <!-- 搜索区域 -->
            <ul class="search-container">
                <li>
                    <span class="search-item-name">手机号：</span>
                    <el-input
                        class="search-item-content search-item-text"
                        placeholder="请输入手机号"
                        v-model="phone"
                        clearable
                        maxlength="11"
                        @clear="clearPhoneFn"
                    >
                    </el-input>
                </li>
                <li>
                    <span class="search-item-name">收件人：</span>
                    <el-input
                        class="search-item-content search-item-text"
                        placeholder="请输入收件人名"
                        v-model="userName"
                        clearable
                        @clear="clearUserName"
                    >
                    </el-input>
                </li>
                <li>
                    <span class="search-item-name">订单编号：</span>
                    <el-input
                        class="search-item-content search-item-text"
                        placeholder="请输入订单编号"
                        v-model="orderNo"
                        clearable
                        @clear="clearOrderNo"
                    >
                    </el-input>
                </li>
                <li>
                    <span class="search-item-name">发货时间：</span>
                    <el-date-picker
                        v-model="date"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        @change="changeDate">
                    </el-date-picker>
                </li>
                <li>
                    <span class="search-item-name">物流状态：</span>
                    <el-select
                        class="search-item-content"
                        v-model="state" 
                        placeholder="请选择"
                        clearable
                        @change="changeState"
                    >
                        <el-option
                            v-for="item in stateOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        >
                        </el-option>
                    </el-select>
                </li>
                <li>
                    <el-button class="search-btn" type="primary" icon="el-icon-search" @click="searchFn" v-if="searchBtn">查询</el-button>
                    <download-excel
                        class = "export-excel-wrapper"
                        :data = "jsonData"
                        :fields = "jsonFields"
                        name = "物流信息表.xls">
                        <el-button class="search-btn" type="primary" icon="el-icon-download" v-if="exportBtn">导出</el-button>
                    </download-excel>
                </li>
            </ul>
        </div>

        <div class="content-bottom-box">
            <!-- 表格 -->
            <div class="content-form-box">
                <el-table
                    v-loading="loadingFlag"
                    :data="tableData"
                    :height="tableHeight"
                    ref="table"
                    border
                    stripe
                    style="width: 100%"
                >
                    <el-table-column
                        prop="serialNumber"
                        label="序号"
                        width="60"
                        :show-overflow-tooltip="true"
                        fixed
                        >
                    </el-table-column>
                    <el-table-column
                        prop="outTradeNo"
                        label="订单编号"
                        width="180"
                        :show-overflow-tooltip="true"
                        fixed
                        >
                    </el-table-column>
                    <el-table-column
                        prop="shipmentTime"
                        label="发货时间"
                        width="160"
                        :show-overflow-tooltip="true"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="addressName"
                        label="收件人"
                        width="120"
                        :show-overflow-tooltip="true"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="addressPhone"
                        label="手机号"
                        width="120"
                        :show-overflow-tooltip="true"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="orderCategoryText"
                        label="类别"
                        width="180"
                        :show-overflow-tooltip="true"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="productName"
                        label="物品"
                        width="180"
                        :show-overflow-tooltip="true"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="quantity"
                        label="数量"
                        :show-overflow-tooltip="true"
                        width="80"
                        >
                    </el-table-column>
                    <el-table-column
                        prop="totalWeight"
                        label="重量"
                        width="100"
                        :show-overflow-tooltip="true"
                        >
                    </el-table-column>
                    <el-table-column
                        label="物流状态"
                        width="120"
                        :show-overflow-tooltip="true"
                        >
                        <template slot-scope="scope">
                            <span>{{scope.row.logisticsStatus == 2 ? '待收货' : '已收货'}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="shipmentId"
                        label="物流信息"
                        width="100"
                        :show-overflow-tooltip="true"
                        >
                        <template slot-scope="scope">
                            <el-button @click="checkInfoFn(scope.row.id, scope.row.type, scope.row.addressText)" type="text" size="small">【查看】</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="addressText"
                        label="收件地址"
                        :show-overflow-tooltip="true"
                        >
                    </el-table-column>
                </el-table>
            </div>

            <!-- 页码 -->
            <div class="content-Page-container">
                <BaseFormPage 
                    :pagination="pagination"
                    @cFirstPageFn="cFirstPageFn"
                    @cLastPageFn="cLastPageFn"
                    @cJumpPageFn="cJumpPageFn"
                    @cPreFn="cPreFn"
                    @cNextFn="cNextFn"
                    @cRefreshFn="cRefreshFn"
                    >
                </BaseFormPage>
            </div>
        </div>

        <el-dialog
            title="物流信息"
            :visible.sync="dialogFlag"
            width="483px"
            center
            :show-close="false"
            class="logistics-wrap">
            <div class="logistics-box">
                <p class="logistics-box-title">物流位置信息 <i class="color-primary" v-if="activities.length <= 0">暂无物流信息</i></p>
                <p class="logistics-address-box">
                    <span>收件地址：</span>
                    <span>{{dialogAddress}}</span>
                </p>
                <div class="time-line">
                    <el-timeline>
                        <el-timeline-item
                            v-for="(activity, index) in activities"
                            :key="index"
                            :icon="activity.icon"
                            :type="activity.type"
                            :color="activity.color"
                            :size="activity.size"
                            :timestamp="activity.timestamp">
                            {{activity.content}}
                        </el-timeline-item>
                    </el-timeline>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" type="primary" @click="closeLogisticsFn">关 闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import Storage from "@/utils/storage";
import BaseFormPage from '@/components/BaseFormPage';
export default {
    components: {
        BaseFormPage
    },
    data() {
        return {
            loadingFlag: false,         //表格数据加载状态
            dialogFlag: false,          //弹窗显示状态
            phone: '',
            userName: '',
            orderNo: '',
            date: '',
            state: '',
            stateOptions: [
                // {
                //     value: 1,
                //     label: '待发货'
                // },
                {
                    value: 2,
                    label: '待收货'
                },
                {
                    value: 3,
                    label: '已收货'
                },
                // {
                //     value: 4,
                //     label: '已发货'
                // },
            ],

            pageIndex: 1,
            pageSize: 20,
            tableHeight: 200,           //表格初始高度
            tableData: [],              //表格数据
            pagination: {               //分页数据
                loadingFlag: false,     //数据加载状态
                currentPage: 1,         //当前页
                totalPage: 1,           //总页数
                totalNum: 0,            //总条数
                firstNum: 0,            //分页显示起始值
                endNum: 0               //分页显示结束值
            },

            jsonFields: {
                "序号": "index",
                "订单编号": "outTradeNo",
                "发货时间": "shipmentTime",
                "收件人": "addressName",
                "手机号": "addressPhone",
                "类别": "orderCategoryText",
                "物品": "productName",
                "数量": "quantity",
                "重量": "totalWeight",
                "物流状态": "logisticsStatusText",
                "收件地址": "addressText",
            },
            jsonData: [],
            jsonMeta: [
                [
                    {
                        "key": "charset",
                        "value": "utf-8"
                    }
                ]
            ],


            dialogAddress: '',

            activities: [
                // {
                //     content: '快递员已完成送货，请确认收货',
                //     timestamp: '2018-04-12 20:46',
                //     size: 'large',
                //     type: 'primary',
                // }, {
                //     content: '已发往广东省 深圳市 福田区沿河路中央西谷大厦607室福田区沿河路中央西谷大厦607室',
                //     timestamp: '2018-04-03 20:46',
                //     size: 'large'
                // }, {
                //     content: '深圳中转站已发出',
                //     timestamp: '2018-04-03 20:46',
                //     size: 'large'
                // }, {
                //     content: '甘肃 兰州中转站已发出',
                //     timestamp: '2018-04-03 20:46',
                //     size: 'large'
                // }, {
                //     content: '甘肃 旦马牧场物流已发出',
                //     timestamp: '2018-04-03 20:46',
                //     size: 'large'
                // }
            ],

            searchBtn: (JSON.parse(Storage.get('roleAuthIdArr'))).indexOf(246) == -1 ? false : true,
            exportBtn: (JSON.parse(Storage.get('roleAuthIdArr'))).indexOf(247) == -1 ? false : true,
        }
    },
    created() {
        this.getListData()
    },
    methods: {
        getListData() {
            this.loadingFlag = true;
            this.pagination.loadingFlag = true;
            let params = {
                phone: this.phone,
                userName: this.userName,
                actRefNo: this.orderNo,
                logisticsStatus: this.state,
                startCreateTime: this.startTime,
                endCreateTime: this.endTime,
                pgCt: this.pageIndex,
                pgSz: this.pageSize
            }
            if(!this.phone) {
                delete params.phone
            }
            if(!this.userName) {
                delete params.userName
            }
            if(!this.orderNo) {
                delete params.actRefNo
            }
            if(!this.state) {
                delete params.logisticsStatus
            }
            if(!this.date) {
                delete params.startCreateTime
                delete params.endCreateTime
            }

            this.$http.get('/logistics/listLogistics', params)
                .then((res) => {
                    if(res.data.records.length > 0) {
                        this.pagination.totalPage = res.data.pages
                        this.pagination.totalNum = res.data.total
                        
                        res.data.records.forEach((item, index) => {
                            //列表下标
                            item.index = index
                            //列表序号
                            item.serialNumber = ((this.pageIndex - 1) * this.pageSize) + (index + 1)
                            item.orderCategoryText = item.orderCategory == 1 ? '采购' : '转售'
                            item.addressText = item.addressProvince + item.addressCity + item.addressDetail
                        })
                        this.pagination.firstNum = res.data.records[0].serialNumber
                        this.pagination.endNum = res.data.records[res.data.records.length-1].serialNumber
                    }else{
                        this.pagination.currentPage = 1
                        this.pagination.totalPage = 1
                        this.pagination.totalNum = 0
                        this.pagination.firstNum = 0
                        this.pagination.endNum = 0
                    }
                    this.tableData  = res.data.records;
                }).then(() => {
                    this.tableHeightFn();
                    this.loadingFlag = false;
                    this.pagination.loadingFlag = false;
                    this.getAllListData()
                })
        },
        getAllListData() {
            let params = {
                phone: this.phone,
                userName: this.userName,
                actRefNo: this.orderNo,
                logisticsStatus: this.state,
                startCreateTime: this.startTime,
                endCreateTime: this.endTime,
                pgCt: 1,
                pgSz: 999999999
            }
            if(!this.phone) {
                delete params.phone
            }
            if(!this.userName) {
                delete params.userName
            }
            if(!this.orderNo) {
                delete params.actRefNo
            }
            if(!this.state) {
                delete params.logisticsStatus
            }
            if(!this.date) {
                delete params.startTime
                delete params.endTime
            }

            this.$http.get('/logistics/listLogistics', params)
                .then((res) => {
                    if(res.data.records.length > 0) {
                        res.data.records.forEach((item, index) => {
                            item.index = index + 1
                            item.orderCategoryText = item.orderCategory == 1 ? '采购' : '转售'
                            item.addressText = item.addressProvince + item.addressCity + item.addressDetail
                            item.logisticsStatusText = item.logisticsStatus == 2 ? '待收货' : '已收货'
                        })
                    }
                    this.jsonData = res.data.records
                })
        },
        tableHeightFn() { //表格高度
            let tableDataLength = this.tableData.length;
            let minHeight = tableDataLength == 0 ? 0 : 80;
            this.$nextTick(function () {
                this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - minHeight;
                if(this.tableData.length <= 0) {
                    this.tableHeight = '60vh'
                }
                
                let self = this;
                window.onresize = function() {
                    self.tableHeight = window.innerHeight - self.$refs.table.$el.offsetTop - minHeight;
                    self.tableHeight = self.tableHeight < 200 ? 200 : self.tableHeight;
                }
            })
        },
        resetPageIndex() { //重置页码
            this.pagination.currentPage = 1;
            this.pageIndex = 1;
        },
        clearPhoneFn() { //清空手机号
            this.searchFn()
        },
        clearUserName() { //清空姓名
            this.searchFn()
        },
        clearOrderNo() { //清空订单编号
            this.searchFn()
        },
        changeDate() { //发货时间
            console.log('date:', this.date)
            if(this.date) {
                this.startTime = this.date[0]
                this.endTime = this.date[1].replace('00:00:00', '23:59:59')
            }else{
                this.startTime = ''
                this.endTime = ''
            }
            this.searchFn()
        },
        changeState() { //物流状态
            console.log('changeState：', this.state)
            this.searchFn()
        },
        searchFn() { //搜索
            this.resetPageIndex()
            this.getListData()
        },

        checkInfoFn(id, type, address) {
            this.dialogFlag = true;
            this.dialogAddress = address;
            
            this.getLogisticsInfoFn(id, type)
        },
        getLogisticsInfoFn(id, type) { //获取物流信息详情 type 1牛订单 2木耳订单
            this.$http.get(`/logistics/exp/id=${id}&&type=${type}`)
                .then((res) => {
                    let infoData = JSON.parse(res.data)
                    if(infoData.Traces.length > 0){
                        infoData.Traces.reverse(); 
                        infoData.Traces.forEach(item => {
                            item.content = item.AcceptStation
                            item.timestamp = item.AcceptTime
                            item.size = 'large'
                        })
                        infoData.Traces[0].type = 'primary';
                    }
                    
                    this.activities = infoData.Traces;
                })
        },
        closeLogisticsFn() {
            this.dialogFlag = false;
            setTimeout(() => {
                this.activities = [];
            }, 300)
        },

        cFirstPageFn(pageNum) { //首页
            this.pageIndex = pageNum;
            this.getListData()
        },
        cLastPageFn(pageNum) { //尾页
            this.pageIndex = pageNum;
            this.getListData()
        },
        cJumpPageFn(pageNum) { //页码跳转
            this.pageIndex = pageNum;
            this.getListData()
        },
        cPreFn(pageNum) { //上一页
            this.pageIndex = pageNum;
            this.getListData()
        },
        cNextFn(pageNum) { //下一页
            this.pageIndex = pageNum;
            this.getListData()
        },
        resetParams() { //重置参数
            this.title = ''
            this.date = ''
            this.startTime = ''
            this.endTime = ''
            this.detailData = {}
            this.tableData = []
            this.pageIndex = 1
            this.pageSize = 20
            this.pagination = {
                loadingFlag: false,
                currentPage: 1,
                totalPage: 1,
                totalNum: 0,
                firstNum: 0,
                endNum: 0
            }
        },
        cRefreshFn() { //刷新
            this.resetParams()
            this.getListData()
        },
    }
}
</script>

<style lang="scss">
    .logistics-wrap {
        .el-dialog {
            .el-dialog__body {
                padding: 24px 40px;
            }
        }
        .logistics-box {
            .logistics-box-title {
                margin-bottom: 20px;
                color: #344047;
                font-weight: bold;
                i {
                    margin-left: 20px;
                }
            }
            .logistics-address-box {
                margin-bottom: 36px;
                color: #5D6164;
                display: flex;
                span {
                    &:nth-of-type(1) {
                        width: 70px;
                    }
                    &:nth-of-type(2) {
                        flex: 1;
                    }
                }
            }
            .time-line {
                padding-left: 20px;
            }
        }
    }
</style>